package com.alen.sun.service.Impl;

import com.alen.sun.bean.Elderly;
import com.alen.sun.bean.Employee;
import com.alen.sun.bean.PageBean;
import com.alen.sun.dao.ElderlyMapper;
import com.alen.sun.service.ElderlyService;
import com.alen.sun.util.PhoneCode;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.org.apache.xpath.internal.operations.Bool;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class ElderlyServiceImpl implements ElderlyService {

    @Autowired
    private ElderlyMapper elderlyMapper;


    @Override
    public Elderly login(Elderly elderly) {
        if (elderly.getPhoneNum() != null && !elderly.getPhoneNum().isEmpty()
                && elderly.getPassword() != null && !elderly.getPassword().isEmpty()) {

            elderly.setPassword(DigestUtils.md5Hex(elderly.getPassword()));
            //数据库查找记录
            Elderly elderly1 = elderlyMapper.selectPhoneNum(elderly);
            return elderly1;
        }else {
            return null;
        }
    }


    @Override
    public Boolean add(Elderly elderly) {
        Optional<String>[] optionals = new Optional[]{
                Optional.ofNullable(elderly.getName()),
                Optional.ofNullable(elderly.getGender()),
                Optional.ofNullable(elderly.getPhoneNum()),
                Optional.ofNullable(elderly.getPassword())
        };

        if (Arrays.stream(optionals).allMatch(opt -> opt.isPresent() && !opt.get().isEmpty()) && elderly.getAge() > 0) {
            //先判断该手机号是否已注册
            Elderly e = elderlyMapper.selectPhoneNum(elderly);
            if(e == null){
                //该手机号未注册
                elderly.setElderlyId(PhoneCode.generateCode());//设置老人ID为6位随机数
                //密码加密
                String encryptedPassword = DigestUtils.md5Hex(elderly.getPassword());
                elderly.setPassword(encryptedPassword);
                elderlyMapper.add(elderly);
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    @Override
    public List<Elderly> getElderlysForEmployee(String id) {
        List<Elderly> elderlies=elderlyMapper.getElderlysForEmployee(id);
        return elderlies;
    }

    @Override
    public Elderly getById(String id) {
        Elderly elderly=elderlyMapper.getById(id);
        return elderly;
    }

    @Override
    public Boolean delete(String id) {
        Boolean flag=elderlyMapper.delete(id);
        return flag;
    }

    @Override
    public Boolean update(Elderly elderly) {
        Boolean flag=elderlyMapper.update(elderly);
        return flag;
    }

    @Override
    public PageBean page(Integer page, Integer pageSize, String word) {
        //1. 设置分页参数
        PageHelper.startPage(page,pageSize);
        List<Elderly> elderlies;
        //2. 执行查询
        if (word==null||word.equals("")){
            elderlies=elderlyMapper.list();
        }else {
            word = "%" + word + "%";
            elderlies = elderlyMapper.selectByWord(word);
        }
        Page<Elderly> p = (Page<Elderly>) elderlies;

        //3. 封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    @Override
    public Boolean deleteByIds(List<String> ids) {
        Boolean flag = elderlyMapper.deleteByIds(ids);
        return flag;
    }
}
